{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="style"}
<style>
	.layui-unselect dl {
		max-height: 188px;
	}
</style>
{/block}
{block name="body"}
<div id="documentList" class="p-3">
	<div class="gg-form-bar border-t border-x">
		<form id="documentForm" class="layui-form">
			<div class="layui-input-inline" style="width:240px;">
				<input type="text" name="project_name" placeholder="所属项目" readonly class="layui-input picker-project" autocomplete="off" />
				<input name="project_id" style="display: none;" value="" />
			</div>
			<div class="layui-input-inline" style="width:300px;">
				<input type="text" name="keywords" placeholder="关键字" class="layui-input" autocomplete="off" />
			</div>
			<div class="layui-input-inline" style="width:150px;">
				<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
				<button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
			</div>
		</form>
	</div>
	<table class="layui-hide" id="document" lay-filter="document"></table>
</div>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
  	<button class="layui-btn layui-btn-sm" lay-event="add">+ 新建文档</button>
  </div>
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const moduleInit = ['tool','tablePlus','oaTool'];
	function gouguInit() {
		var table = layui.tablePlus, form = layui.form, tool = layui.tool;

		layui.documentTable = table.render({
			elem: '#document',
			title: '文档列表',
			toolbar: '#toolbarDemo',
			defaultToolbar:false,
			is_excel:true,
			cellMinWidth: 80,
			url: "/project/document/index",
			page: true, //开启分页
			limit: 20,
			cols: [[
				{
					field: 'id', title: '文档编号', width: 90, align: 'center', templet: function (d) {
						return 'D' + d.id;
					}
				}
				, {
					field: 'title', title: '文档主题',minWidth:240, templet: function (d) {
						var html = '<a class="side-a" data-href="/project/document/view/id/' + d.id + '">' + d.title + '</a>';
						return html;
					}
				}
				, { field: 'project_name', title: '关联项目', width: 300,}
				, { field: 'admin_name', title: '创建人', align: 'center', width: 80 }
				, { field: 'create_time', title: '创建时间', align: 'center', width: 132 }
				,{
						field: 'right',
						fixed:'right',
						title: '操作',
						width: 100,
						align: 'center',
						templet: function (d) {
							var html = '<div class="layui-btn-group">';
							var btn1='<span class="layui-btn layui-btn-xs" lay-event="edit">编辑</span>';
							var btn2='<span class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</span>';
							return html+btn1+btn2+'</div>';
						}						
					}
			]]
		});
		
		
		//触发事件
		table.on('toolbar(document)', function(obj){
		  var checkStatus = table.checkStatus(obj.config.id);
		  switch(obj.event){
			case 'add':
			  tool.side('/project/document/add');
			break;
		  };
		});
		
		//监听行工具事件
		table.on('tool(document)', function(obj) {
			var data = obj.data;			
			if(obj.event === 'edit'){
				tool.side('/project/document/add?id='+data.id);
				return;
			}
			if (obj.event === 'del') {
				layer.confirm('确定要删除吗?', {
					icon: 3,
					title: '提示'
				}, function(index) {
					let callback = function (e) {
						layer.msg(e.msg);
						if (e.code == 0) {
							obj.del();
						}
					}
					tool.delete("/project/document/delete", {id: data.id}, callback);
					layer.close(index);
				});
			}
		});

		form.on('select(project)', function (data) {
			tableReload();
		});
		//监听搜索提交
		form.on('submit(webform)', function (data) {
			if (data.field.keywords == '') {
				return false;
			}
			tableReload();
			return false;
		});
		$('#documentForm').on('click', '[lay-filter="clear"]', function () {
			setTimeout(function () {
				tableReload();
			}, 10)
		})

		function tableReload() {
			let postData = {
				project_id: $('#documentForm').find('[name="project_id"]').val(),
				keywords: $('#documentForm').find('[name="keywords"]').val()
			}
			layui.documentTable.reload({ where: postData });
		}
	}		
</script>
{/block}
<!-- /脚本 -->